#[cfg(test)]
mod tests {
    /// 测试声明式宏与过程宏的基本用法
    /// 验证宏展开机制与卫生性(hygiene)特性
    /// 演示元变量(macro_rules!)在模式匹配中的应用
    #[test]
    fn test_macro() {
        macro_rules! add {
            ($a:expr, $b:expr) => {
                $a + $b
            };
        }
        let result = add!(1, 2);
        assert_eq!(result, 3);

        // 声明式宏进阶测试
        macro_rules! vector {
            ($($x:expr),*) => {
                {
                    let mut temp_vec = Vec::new();
                    $(temp_vec.push($x);)*
                    temp_vec
                }
            };
        }
        let v = vector![1, 2, 3];
        assert_eq!(v, vec![1, 2, 3]);
    }

    /// 测试Result错误处理机制
    /// 验证错误传播运算符(?)的使用
    /// 演示自定义错误类型与错误转换
    #[test]
    fn test_error_handling() {
        fn divide(a: i32, b: i32) -> Result<i32, &'static str> {
            if b == 0 {
                Err("division by zero")
            } else {
                Ok(a / b)
            }
        }
        let result = divide(10, 2);
        assert_eq!(result, Ok(5));
        let result = divide(10, 0);
        assert_eq!(result, Err("division by zero"));

        // 错误传播测试
        fn read_file() -> Result<String, &'static str> {
            let content = "file content";
            Ok(content.to_string())
        }

        fn process_data() -> Result<usize, &'static str> {
            let content = read_file()?;
            Ok(content.len())
        }

        assert_eq!(process_data(), Ok(11));

        // 自定义错误类型测试
        #[derive(Debug, PartialEq)]
        enum CustomError {
            InvalidInput,
            Overflow
        }

        fn safe_add(a: u32, b: u32) -> Result<u32, CustomError> {
            a.checked_add(b).ok_or(CustomError::Overflow)
        }

        assert_eq!(safe_add(1, 2), Ok(3));
        assert_eq!(safe_add(u32::MAX, 1), Err(CustomError::Overflow));
    }

    /// 测试泛型类型推断机制
    /// 验证单态化(monomorphization)过程
    /// 演示泛型函数在编译时的类型安全检查
    #[test]
    fn test_generics() {
        fn identity<T>(x: T) -> T {
            x
        }
        let result = identity(5);
        assert_eq!(result, 5);
    }

    #[test]
    fn test_pattern_guard() {
        let pair = (2, -2);
        match pair {
            (x, y) if x == y => println!("Twins"),
            (x, y) if x + y == 0 => println!("Antipodes"),
            _ => println!("Ordinary pair")
        }
        assert!(matches!(pair, (x, y) if x + y == 0));
    }

    #[test]
    fn test_iterator_combinators() {
        let v = vec![1, 2, 3, 4, 5];
        let sum = v.iter()
            .filter(|&x| x % 2 == 0)
            .map(|x| x * 3)
            .fold(0, |acc, x| acc + x);
        assert_eq!(sum, 18);
    }
}